package com.rongshu.view.home.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.rongshu.R;
import com.rongshu.utils.GlideUtils;
import com.rongshu.view.home.entity.ArticleItem;
import com.rongshu.view.home.listener.OnTodayRecommendListener;
import com.rongshu.widget.ArticleLabelView;
import com.utils.FastClickUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TodayRecommendListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //Context
    private Context context = null;
    //list
    private List<ArticleItem> articleItemList = null;
    //listener
    private OnTodayRecommendListener mOnTodayRecommendListener = null;

    public TodayRecommendListAdapter(Context context) {
        this.context = context;
    }

    /**
     * 设置推荐列表
     */
    public void setArticleItemList(List<ArticleItem> articleItemList) {
        this.articleItemList = articleItemList;
    }

    /**
     * 添加推荐列表
     */
    public void addArticleItemList(List<ArticleItem> articleItemList) {
        this.articleItemList.addAll(articleItemList);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_today_recommend_item_view, viewGroup, false);
        return new TodayRecommentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        ArticleItem bean = articleItemList.get(i);
        TodayRecommentViewHolder holder = (TodayRecommentViewHolder) viewHolder;
        //image
        String topImg = bean.getTopImg();
        if (TextUtils.isEmpty(topImg)) {
            holder.article_img.setVisibility(View.GONE);
        } else {
            holder.article_img.setVisibility(View.VISIBLE);
            GlideUtils.loadImageView(context, holder.article_img, topImg,
                    R.drawable.default_preview_icon, R.drawable.default_preview_icon);
        }
        //title
        holder.article_title.setText(bean.getTitle());
        holder.article_title.setTextColor(bean.isBrowse() ?
                ContextCompat.getColor(context, R.color.color_898989) :
                ContextCompat.getColor(context, R.color.color_2c2c2c));
        //content
        holder.article_content.setText(bean.getContents_header());
        //author
        holder.article_author.setText(bean.getBi_name());
        //category
        holder.article_category.setText(bean.getCategories());
        //label
        holder.articleLabel.bindDatas(bean.getKeywordList());
        //listener
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTodayRecommendListener != null && !FastClickUtils.isFastClick()) {
                    mOnTodayRecommendListener.onItemClick(i);
                }
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return articleItemList.isEmpty() ? 0 : articleItemList.size();
    }

    public class TodayRecommentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.article_img)
        ImageView article_img;
        @BindView(R.id.article_title)
        TextView article_title;
        @BindView(R.id.article_content)
        TextView article_content;
        @BindView(R.id.article_author)
        TextView article_author;
        @BindView(R.id.article_category)
        TextView article_category;
        @BindView(R.id.article_label)
        ArticleLabelView articleLabel;

        public TodayRecommentViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * set listener
     *
     * @param listener
     */
    public void setOnTodayRecommendListener(OnTodayRecommendListener listener) {
        this.mOnTodayRecommendListener = listener;
    }
}
